package co.yixiang.business.demo

import co.yixiang.anno.NoArg
import co.yixiang.util.MBaseTable
import com.baomidou.mybatisplus.annotation.*
import com.fasterxml.jackson.annotation.JsonFormat
import me.liuwj.ktorm.dsl.QueryRowSet
import me.liuwj.ktorm.schema.int
import me.liuwj.ktorm.schema.long
import me.liuwj.ktorm.schema.varchar
import me.liuwj.ktorm.support.mysql.insertOrUpdate
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate
import java.time.LocalDateTime
import javax.persistence.*

object Demos : MBaseTable<DemoSql>("fly_demo", null) {

    val name = varchar("name")
    val title = varchar("title")
    val studentId = long("student_id")


    override fun doCreateEntity(row: QueryRowSet, withReferences: Boolean): DemoSql {

        return DemoSql(
            id = row[id],
            title = row[title],
            name = row[name],
            studentId = row[studentId],
            createTime = row[createTime]!!,
            updateTime = row[updateTime],
            createBy = row[createBy],
            deleted = row[deleted]!!
        )
    }
}

@Entity
@Table(name = "fly_demo")
@org.hibernate.annotations.Table(appliesTo = "fly_demo", comment = "测试表")
@DynamicInsert
@DynamicUpdate
@TableName("fly_demo")
data class DemoSql(

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @TableId
    var id: Long? = null,

    @Column(columnDefinition = "varchar(100) default '' comment '用户名'")
    var name: String? = "",

    @Column(columnDefinition = "varchar(100) default '' comment '标题'")
    var title: String? = "",

    @Column(columnDefinition = "int(11) comment '学成id'")
    var studentId: Long? = null,

    @Column(columnDefinition = "int(11) comment '创建者'")
    var createBy: Long? = null,

    @Column(name = "create_time", nullable = false)
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    var createTime: LocalDateTime = LocalDateTime.now(),

    @Column(name = "update_time", nullable = true)
    @TableField(fill = FieldFill.UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    var updateTime: LocalDateTime? = LocalDateTime.now(),

    @Column(name = "deleted", nullable = false)
    @TableLogic
    var deleted: Boolean = false
)
